//package com.cq.hd.admin.shiro;
//
//import com.cq.hd.admin.po.TbAdminUserPo;
//import com.cq.hd.admin.service.TbAdminPermissionService;
//import com.cq.hd.admin.service.TbAdminUserService;
//import com.cq.hd.admin.utils.StringUtil;
//import org.apache.shiro.authc.*;
//import org.apache.shiro.authz.AuthorizationException;
//import org.apache.shiro.authz.AuthorizationInfo;
//import org.apache.shiro.authz.SimpleAuthorizationInfo;
//import org.apache.shiro.realm.AuthorizingRealm;
//import org.apache.shiro.subject.PrincipalCollection;
//import org.slf4j.Logger;
//import org.slf4j.LoggerFactory;
//import org.springframework.stereotype.Component;
//import org.springframework.util.StringUtils;
//
//import javax.annotation.Resource;
//import java.util.HashSet;
//import java.util.Set;
//
//@Component
//public class DbShiroRealm extends AuthorizingRealm {
//
//    @Resource
//    private TbAdminUserService adminUserService;
//    @Resource
//    private TbAdminPermissionService adminPermissionService;
//
//    @Override
//    public boolean supports(AuthenticationToken token) {
//        return token instanceof UsernamePasswordToken;
//    }
//
//    @Override
//    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {
//        UsernamePasswordToken token;
//        if (authenticationToken instanceof UsernamePasswordToken) {
//            token = (UsernamePasswordToken) authenticationToken;
//        } else {
//            return null;
//        }
//
//        String userName = token.getUsername();
//        String password = new String(token.getPassword());
//        if (StringUtils.isEmpty(userName)) {
//            throw new AccountException("用户名不能为空！");
//        }
//        if (StringUtils.isEmpty(password)) {
//            throw new AccountException("密码不能为空！");
//        }
//        TbAdminUserPo user = adminUserService.getUserByUserName(userName);
//        if (user == null) {
//            throw new UnknownAccountException("用户名或密码错误！");
//        }
//
//        String encodedPassword = StringUtil.SHA1(userName + password);
//        if (StringUtils.isEmpty(encodedPassword) || !(encodedPassword.equals(user.getPassword()))) {
//            throw new UnknownAccountException("用户名或密码错误！");
//        }
//        if (user.getStatus() == null || user.getStatus() != 0) {
//            throw new UnknownAccountException("您已被禁用！");
//        }
//
//        return new SimpleAuthenticationInfo(user, password, this.getName());
//    }
//
//    @Override
//    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
//        if (principals == null) {
//            throw new AuthorizationException("PrincipalCollection method argument cannot be null.");
//        }
//        TbAdminUserPo adminUserEntity = (TbAdminUserPo) getAvailablePrincipal(principals);
//        Integer roleIds = adminUserEntity.getRoleId();
//        Set<String> roleSet = new HashSet<>();
//        roleSet.add(roleIds.toString());
//        Set<String> permissions = adminPermissionService.getSetByRoleIds(roleIds);
//        SimpleAuthorizationInfo simpleAuthorizationInfo = new SimpleAuthorizationInfo();
//        simpleAuthorizationInfo.setRoles(roleSet);
//        simpleAuthorizationInfo.setStringPermissions(permissions);
//        return simpleAuthorizationInfo;
//    }
//
//
//}
